package com.artron.mediaartron.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.helper.BoxingResHelper;
import com.artron.mediaartron.ui.widget.MediaImageLayout;
import com.bilibili.boxing.model.BoxingManager;
import com.bilibili.boxing.model.config.BoxingConfig;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;

import java.util.ArrayList;
import java.util.List;


/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-27.
 */

public class PictureSelectionAdapter extends RecyclerView.Adapter {

    private static final int CAMERA_TYPE = 0;
    private static final int NORMAL_TYPE = 1;

    private final LayoutInflater mInflater;
    private final List<BaseMedia> mMedias;
    private final List<BaseMedia> mSelectedMedias;
    private final BoxingConfig mMediaConfig;
    private int mOffset;
    private final boolean mMultiImageMode;
    private final int mDefaultRes;

    private boolean isNeedCamera = true;

    private View.OnClickListener mOnCameraClickListener;
    private View.OnClickListener mOnMediaClickListener;
    private OnCheckListener mOnCheckListener;
    private OnMediaCheckedListener mOnCheckedListener;

    public PictureSelectionAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.mMedias = new ArrayList<>();
        this.mSelectedMedias = new ArrayList<>();
        this.mMediaConfig = BoxingManager.getInstance().getBoxingConfig();
        this.mOffset = mMediaConfig.isNeedCamera() && isNeedCamera ? 1 : 0;
        this.mMultiImageMode = mMediaConfig.getMode() == BoxingConfig.Mode.MULTI_IMG;
        this.mDefaultRes = mMediaConfig.getMediaPlaceHolderRes();
        this.mOnCheckListener = new OnCheckListener();
    }

    public void setNeedCamera(boolean needCamera) {
        isNeedCamera = needCamera;
        mOffset = mMediaConfig.isNeedCamera() && isNeedCamera ? 1 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && mMediaConfig.isNeedCamera() && isNeedCamera) {
            return CAMERA_TYPE;
        }
        return NORMAL_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (CAMERA_TYPE == viewType) {
            return new CameraViewHolder(mInflater.inflate(R.layout.item_picture_selection_camera,
                    parent, false));
        }

        return new ImageViewHolder(mInflater.inflate(R.layout.item_picture_selection_image,
                parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CameraViewHolder) {
            CameraViewHolder cameraHolder = (CameraViewHolder) holder;
            cameraHolder.mParent.setOnClickListener(mOnCameraClickListener);
            cameraHolder.mIvCamera.setImageResource(BoxingResHelper.getCameraRes());
        } else {
            int pos = position - mOffset;
            BaseMedia baseMedia = mMedias.get(pos);
            ImageViewHolder imageHolder = (ImageViewHolder) holder;

            imageHolder.mMediaLayout.setImageRes(mDefaultRes);
            imageHolder.mMediaLayout.setTag(baseMedia);

            imageHolder.mMediaLayout.setOnClickListener(mOnMediaClickListener);
            imageHolder.mMediaLayout.setTag(R.id.PictureSelectionFragmentItem_iv_check, pos);
            imageHolder.mMediaLayout.setMedia(baseMedia);
//            imageHolder.mIvCheck.setVisibility(mMultiImageMode ? View.VISIBLE : View.GONE);
            if (/*mMultiImageMode && */baseMedia instanceof ImageMedia) {
                imageHolder.mMediaLayout.setChecked(((ImageMedia) baseMedia).isSelected());
                imageHolder.mIvCheck.setTag(R.id.PictureSelectionFragmentItem_media_layout,
                        imageHolder.mMediaLayout);
                imageHolder.mIvCheck.setTag(baseMedia);
                imageHolder.mIvCheck.setOnClickListener(mOnCheckListener);
            }

        }
    }



    @Override
    public int getItemCount() {
        return mMedias.size() + mOffset;
    }

    public void setOnCameraClickListener(View.OnClickListener onCameraClickListener) {
        mOnCameraClickListener = onCameraClickListener;
    }

    public void setOnCheckedListener(OnMediaCheckedListener onCheckedListener) {
        mOnCheckedListener = onCheckedListener;
    }

    public void setOnMediaClickListener(View.OnClickListener onMediaClickListener) {
        mOnMediaClickListener = onMediaClickListener;
    }

    public List<BaseMedia> getSelectedMedias() {
        return mSelectedMedias;
    }

    public void setSelectedMedias(List<BaseMedia> selectedMedias) {
        if (selectedMedias == null) {
            return;
        }
        mSelectedMedias.clear();
        mSelectedMedias.addAll(selectedMedias);
    }

    public void addAllData(@NonNull List<BaseMedia> data) {
        this.mMedias.addAll(data);
        notifyDataSetChanged();
    }

    public void addData(BaseMedia media) {
        this.mMedias.add(media);
        notifyDataSetChanged();
    }
    public void clearData() {
        this.mMedias.clear();
    }

    public List<BaseMedia> getAllMedias() {
        return mMedias;
    }


    private static class CameraViewHolder extends RecyclerView.ViewHolder {

        final View mParent;
        final ImageView mIvCamera;

        CameraViewHolder(View itemView) {
            super(itemView);
            mParent = itemView.findViewById(R.id.PictureSelectionFragmentItem_container);
            mIvCamera = (ImageView) itemView.findViewById(R.id.PictureSelectionFragmentItem_iv_camera);
        }
    }

    private static class ImageViewHolder extends RecyclerView.ViewHolder {

        final MediaImageLayout mMediaLayout;
        final ImageView mIvCheck;


        ImageViewHolder(View itemView) {
            super(itemView);
            mMediaLayout = (MediaImageLayout) itemView.findViewById(R.id.PictureSelectionFragmentItem_media_layout);
            mIvCheck = (ImageView) itemView.findViewById(R.id.PictureSelectionFragmentItem_iv_check);
        }
    }

    public void notifyOffsetItemChanged(int position) {
        notifyItemRangeChanged(position + mOffset, 1);
    }

    private class OnCheckListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            MediaImageLayout itemLayout = (MediaImageLayout) v.getTag(R.id.PictureSelectionFragmentItem_media_layout);
            BaseMedia media = (BaseMedia) v.getTag();
            if (mMediaConfig.getMode() == BoxingConfig.Mode.MULTI_IMG
                    || mMediaConfig.getMode() == BoxingConfig.Mode.SINGLE_IMG) {
                if (mOnCheckedListener != null) {
                    mOnCheckedListener.onChecked(itemLayout, media);
                }
            }
        }
    }

    public interface OnMediaCheckedListener {
        /**
         * In multi image mode, selecting a {@link BaseMedia} or undo.
         */
        void onChecked(View v, BaseMedia iMedia);
    }


}
